<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 节点分类管理类
 * Class Admin_node_group_model
 *
 */
class Admin_node_group_model extends SDF_Model{
    var $title = '';
    var $content = '';
    var $date = '';
    /**
     * 构造函数 初始化数据库操作类
     */
    function __construct(){
        $this->load->database();
        parent::__construct();
    }

    /**
     * 获取指定节点的分组树
     * @param bool $r_cache
     * @return array
     */
    public function get_admin_node_group_tree($r_cache = false){
        $S_KEY = __CLASS__.'-'.__FUNCTION__;
        if($r_cache===true){
            $tree = $this->S($S_KEY);
            if(!empty($tree)){
                return $tree;
            }
        }
        $list_arr = $tree = array();
        $list = $this->db->select('id,name,pid')->order_by('sort_order','desc')->get('admin_node_group')->result_array();
        foreach($list as $row){
            /*if(isset($node_groupid_arr[$row['id']])){
                $row['nodes'] = $node_groupid_arr[$row['id']];
            }*/
            $list_arr[$row['id']] = $row;
        }
        foreach($list_arr as $vo){
            if(isset($list_arr[$vo['pid']] )){
                $list_arr[$vo['pid']]['son'][$vo['id']] = &$list_arr[$vo['id']];
            }else{
                $tree[$vo['id']] = &$list_arr[$vo['id']];
            }
        }
        $this->S($S_KEY,$tree);
        return $tree;
    }

    /**
     * @param int $select_id
     * @param bool $r_cache
     * @return string
     */
    public function get_admin_node_group_option_tree($select_id=0,$r_cache=false){
        $this->option_list_str = '';
        $tree = $this->get_admin_node_group_tree($r_cache);
        $this->get_child_option_tree($tree,$select_id);
        return $this->option_list_str;
        /*foreach($tree as $t){
            $option_list_str .="<option value=\"{$t['id']}\">{$t['name']}</option>";
            if(empty($t['son'])){
                $option_list_str .=$this->get_child tree($tree,$nbsp = '&nbsp;');
            }
        }*/

    }

    /**
     * @param $tree
     * @param int $select_id
     * @param string $nbsp
     */
    private function get_child_option_tree(&$tree,$select_id=0,$nbsp='&nbsp;&nbsp;'){
        foreach($tree as $t){
            $selected_str = ($select_id == $t['id'])?'selected="selected"':'';
            $this->option_list_str .= "<option {$selected_str} value=\"{$t['id']}\">{$nbsp}{$t['name']}({$t['id']})</option>";
            if(isset($t['son']) and is_array($t['son'])){
                $this->get_child_option_tree($t['son'],$select_id,$nbsp.$nbsp);
            }
        }
    }

    /**
     * @param $node_list
     * @return array
     */
    public function get_admin_node_group_menu($node_list){
        $node_groupid_arr = array();
        foreach($node_list as &$node){
            $node_groupid_arr[$node['node_groupid']][] = $node;
        }
        $this->db->where('status',1);
        $tree = $this->get_admin_node_group_tree();
        $this->get_child_node_num($tree,$node_groupid_arr);
        return $tree;
    }

    /**
     * @param $tree
     * @param $node_groupid_arr
     * @return int
     */
    private function get_child_node_num(&$tree,&$node_groupid_arr){
        $child_node_num = 0;
        foreach($tree as &$t){
            $t['child_node_num'] = 0;
            if(isset($node_groupid_arr[$t['id']])){
                $t['nodes'] = $node_groupid_arr[$t['id']];
                $t['child_node_num'] += count($t['nodes']);
            }
            if(isset($t['son']) and is_array($t['son'])){
                $t['child_node_num'] += $this->get_child_node_num($t['son'],$node_groupid_arr);
            }
            $child_node_num += $t['child_node_num'];
        }
        return $child_node_num;
    }

    /**
     * 更具code获取分组id,如果分组不存在，自动创建
     * @param $modules
     * @param $controller
     * @param string $title
     * @return mixed
     */
    public function get_group_id($modules,$controller,$title=''){
        $code = $modules.$controller;
        $this->db->where("FIND_IN_SET('{$code}',`code`)<>",0);
        $query = $this->db->get('admin_node_group',1);
        if ($query->num_rows() > 0){
            $row = $query->row();
            return $row->id;
        }
        $data = array(
            'name' => empty($title)?$code:$title ,
            'status' => 1,
            'code' => $code
        );
        $this->db->insert('admin_node_group', $data);
        return $this->db->insert_id();
    }




}